package helper;

import addressBook.AddressBook;
import addressBook.Contact;
import addressBook.TreeIterator;
import util.ContactReaderUtil;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

/**
 * @author Jarvan
 * @version 1.0
 * @create 2020/11/14 19:00
 */
public class Helper {
    /**
     * test flow method.
     * 1. create an empty address book
     * 2. in a method ,fill the address book
     * 3. in a method ,display the display the address book
     * 4. more.
     */
    public static void start() throws IOException {
        //1. create an empty address book
        AddressBook addressBook = new AddressBook();
        //2. in a method ,fill the address book
        fillAddressBook(addressBook);
        //3. in a method ,display the display the address book
        displayAddressBook(addressBook);
        //4.find contact
        findContact(addressBook);
        //5. delete contact
        deleteContact(addressBook);
        //6. find contact
        findContact(addressBook);



    }
    public static void fillAddressBook(AddressBook addressBook) throws IOException {
        List<Contact> contactList = ContactReaderUtil.getContactList();
        for (Contact contact : contactList) {
            addressBook.insertContact(contact);
        }
    }
    public static void displayAddressBook(AddressBook addressBook){
        TreeIterator<Contact> iterator = addressBook.getTree().iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
    public static void  findContact(AddressBook addressBook){
        System.out.println("=====find contact by name====");
        System.out.println("input name such as :");
        System.out.println("James Butt");
        System.out.println("Josephine Darakjy");
        Scanner scanner = new Scanner(System.in);
        String searchName = scanner.nextLine();
        Contact contact = addressBook.searchContact(searchName);
        if (contact != null) {
            System.out.println("=====search success=====");
            System.out.println(contact);
        }else {
            System.out.println("=====find fail======");
        }
    }
    public static void deleteContact(AddressBook addressBook){
        System.out.println("=======delete contact=======");
        System.out.println("input name(delete) such as :");
        System.out.println("James Butt");
        System.out.println("Josephine Darakjy");
        Scanner scanner = new Scanner(System.in);
        String deleteName = scanner.nextLine();
        boolean b = addressBook.deleteContact(deleteName);
        if (b) {
            System.out.println("====delete success====");
        }else {
            System.out.println("====delete fail====");
        }
    }
}
